import ms from 'ms';
import initServerInstance from '../server/initServerInstance';
import { LIMITERS } from '../server/plugins/billing/constants';

if (require.main === module) {
	(async () => {
		const server = await initServerInstance({
			loggerConfig: {
				name: 'upgrade',
				alwaysCreate: true,
			},
		});

		for await (const record of server.app.model.Balance.find()) {
			Object.keys(record.toObject().balance).forEach(key => {
				const field = record.balance[key];
				if (LIMITERS[key].time && !field.time) {
					field.time = LIMITERS[key].time;
				}
				if (field.time) {
					record.balance[key].time = parseTime(field.time);
				}
				record.balance[key].charges = field.charges.map(x => {
					const c = x.toObject();
					if (c.time) {
						c.time = parseTime(x.time);
					}
					return c;
				});
			});

			record.balance.vulMaxFreq = {
				...LIMITERS.vulMaxFreq,
				total: 0,
				used: 0,
			};
			record.balance.securityEventMaxFreq = {
				...LIMITERS.securityEventMaxFreq,
				total: 0,
				used: 0,
			};
			record.balance.sslMaxFreq = {
				...LIMITERS.sslMaxFreq,
				total: 0,
				used: 0,
			};
			record.balance.assetMaxFreq = {
				...LIMITERS.assetMaxFreq,
				total: 0,
				used: 0,
			};
			if (record.planId) {
				record.balance.vulMaxFreq.charges = [
					{
						...LIMITERS.vulMaxFreq,
						total: 0,
						used: 0,
						chargeType: 'plan',
						ref: record.planId,
					},
				];
				record.balance.securityEventMaxFreq.charges = [
					{
						...LIMITERS.securityEventMaxFreq,
						total: 0,
						used: 0,
						chargeType: 'plan',
						ref: record.planId,
					},
				];
				record.balance.sslMaxFreq.charges = [
					{
						...LIMITERS.sslMaxFreq,
						total: 0,
						used: 0,
						chargeType: 'plan',
						ref: record.planId,
					},
				];
				record.balance.assetMaxFreq.charges = [
					{
						...LIMITERS.assetMaxFreq,
						total: 0,
						used: 0,
						chargeType: 'plan',
						ref: record.planId,
					},
				];
			}

			await record.save().catch(error => console.log(error, record));
		}
		for await (const record of server.app.model.Plan.find()) {
			Object.keys(record.toObject().limit).forEach(key => {
				const field = record.limit[key];
				if (LIMITERS[key].time && !field.time) {
					field.time = LIMITERS[key].time;
				}
				if (field.time) {
					record.limit[key].time = parseTime(field.time);
				}
			});
			record.limit.vulMaxFreq = {
				...LIMITERS.vulMaxFreq,
				total: 0,
				used: 0,
			};
			record.limit.securityEventMaxFreq = {
				...LIMITERS.securityEventMaxFreq,
				total: 0,
				used: 0,
			};
			record.limit.sslMaxFreq = {
				...LIMITERS.sslMaxFreq,
				total: 0,
				used: 0,
			};
			record.limit.assetMaxFreq = {
				...LIMITERS.assetMaxFreq,
				total: 0,
				used: 0,
			};
			await record.save().catch(error => console.log(error, record));
		}
		for await (const record of server.app.model.AdditionPackage.find()) {
			Object.keys(record.toObject().package).forEach(key => {
				const field = record.package[key];
				if (LIMITERS[key].time && !field.time) {
					field.time = LIMITERS[key].time;
				}
				if (field.time) {
					record.package[key].time = parseTime(field.time);
				}
			});
			await record.save().catch(error => console.log(error, record));
		}

		process.exit();
	})();
}

function parseTime(time) {
	const ret = /[a-z]$/i.test(time) ? ms(time) : parseInt(time, 10);
	return ret;
}
